<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('辅助明细')" /> 
    	<th:block th:include="include :: bootstrap-editable-css" />
</head> 
<body class="hold-transition box box-main">

        <input name="index" id="index" type="hidden"  th:value="${index}" />
        <input name="kmdm" id="kmdm" type="hidden"  th:value="${kmdm}" />
        <input name="fzhsdw" id="fzhsdw" type="hidden"  th:value="${fzhsdw}" />
     
        <div class="container-div">
        <div class="btn-group-sm" id="toolbar" role="group">
             <a class="btn btn-success" onclick="insertRow()">
		            <i class="fa fa-plus"></i> 新增行
		        </a>
		        <a class="btn btn-danger multiple disabled" onclick="removeRow()">
		            <i class="fa fa-remove"></i> 删除行
		        </a>
        </div>
		<div class="row">
			<div class="col-sm-12 select-table ">
				<table id="mxtableId"></table>
			</div>
		</div>
        </div>
    <th:block th:include="include :: footer" />
   <th:block th:include="include :: bootstrap-table-editable-js" />
    <script th:inline="javascript">
	    $(function() {
	    	var mxData = new Array();
	    	var fzmx=$(window.parent.document).find("input[name='pztis["+$("#index").val()+"].fzmx']").val();
	    	if (fzmx!=undefined && fzmx!=''){
	    		mxData=$.parseJSON(fzmx);
	    	}
	        var options = {
		        showSearch: false,
		        showRefresh: false,
		        showToggle: false,
		        showColumns: false,
		        pagination: false,
		        striped : true,
		        data: mxData,
		        uniqueId: 'id',
		        height: 350,
		        id:'mxtableId',
		        editable:true,//开启编辑模式  
		        clickToSelect: true,
	            columns: [
	                {   field : 'tid', 
			            checkbox: true
			        },
		            {
					field : 'id', 
					title : '序号',
					visible: false
				},{
					field : 'pjh', 
					title : '票据号',
					width: 80,
					editable : {
						type : 'text',
						title : '票据号',
						emptytext : "录入票据号",
						mode: "inline"
					}
				},{
                    title: '单位选择',
                    align: 'center',
                    align: 'left',
                    width: 40,
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-info  btn-xs" href="javascript:void(0)" onclick="selectHsdwTree(' + index + ')"><i class="fa fa-plus"></i>选择</a> ');
                        return actions.join('');
                    }
                },
				{
					field : 'dwbm', 
					title : '单位编码',
					align: 'center'
				},{
					field : 'dwmc', 
					title : '单位名称',
					align: 'center'
				},{
                    title: '预算选择',
                    align: 'center',
                    align: 'left',
                    width: 40,
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-info  btn-xs" href="javascript:void(0)" onclick="selectysKmdmTree(' + index + ')"><i class="fa fa-plus"></i>选择</a> ');
                        return actions.join('');
                    }
                }
				,{
					field : 'yskm', 
					title : '预算科目',
					align: 'center'
				}
				,{
					field : 'yskmmc', 
					title : '预算科目名称',
					align: 'center'
				}, {
                    title: '项目选择',
                    align: 'center',
                    align: 'left',
                    width: 40,
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-info  btn-xs" href="javascript:void(0)" onclick="selectxmxxTree(' + index + ',\''+row.dwbm+'\')"><i class="fa fa-plus"></i>选择</a> ');
                        return actions.join('');
                    }
                }
				,{
					field : 'xmbm', 
					title : '项目编码',
					align: 'center'
				}
				,{
					field : 'xmmc', 
					title : '项目名称',
					align: 'center'
				},
				{
					field : 'je', 
					title : '金额',
	                align: 'right',
	                width:150,
					editable : {
						type : 'text',
						title : '金额',
						emptytext : "录入金额",
						mode: "inline",
						validate : function(value) {
							   if (!value) return '【金额】不能为空';
			                    var patrn=/^([1-9]\d*\.\d{1,2}|0\.\d[1-9]|[1-9]\d*)$/;
				                if(!patrn.test(value)){
				               		 return '输入格式：最多两位小数';
				                }
						}
					}
				}]
	        };
	        $.table.init(options);
	    });
		
		 
	    function insertRow(){
	    	var randomId = 100 + ~~(Math.random() * 100);
	    	var row={
	            	id:randomId,
	            	pjh: '', 
	            	kmmc: '',
	            	dwbm: '',
	            	dwmc: '',
	            	yskm: '',
	            	yskmmc: '',
	            	xmbm: '',
	            	xmmc: '',
	            	je: ''
	            			
	            };
	    	$("#mxtableId").bootstrapTable('append', row)
	    }
	    
	    /* 删除指定表格行 */
	    function removeRow(){
	    	var ids = $.table.selectColumns("id");
	    	if (ids.length == 0) {
				$.modal.alertWarning("请至少选择一条记录");
				return;
			}
	    	$("#mxtableId").bootstrapTable('remove', {
	    	    field: 'id',
	    	    values: ids
	    	})
	    }
	    
		   /*核算单位-新增-选择父部门树*/
        function selectHsdwTree(upIndex) {
            var options = {
                title: '核算单位选择'+'_'+upIndex,
                width: "380",
                url: ctx + "system/hsdw/selectHsdwTree/",
                callBack: dohsdwSubmit
            };
            $.modal.openOptions(options);
        }

        function dohsdwSubmit(index, layero){
        	var tree = layero.find("iframe")[0].contentWindow.$._tree;
			if ($.tree.notAllowParents(tree)) {
			   var titile=$(".layui-layer-title")[0].innerText;
	     	   var rowIndex=0;
	     	   if (titile!=undefined){
	     		   rowIndex=titile.split('_')[1];
               var body = layer.getChildFrame('body', index);
               var dwbm=body.find('#treeId').val();
               var dwmc=body.find('#treeName').val();
               var rowsdwbm = {
            		   index : rowIndex, //更新列所在行的索引
            		   field : "dwbm", //要更新列的field
            		   value : dwbm //要更新列的数据
            		   }//更新表格数据 
               $("#mxtableId").bootstrapTable("updateCell",rowsdwbm);
               var rowsdwmc = {
            		   index : rowIndex, //更新列所在行的索引
            		   field : "dwmc", //要更新列的field
            		   value : dwmc //要更新列的数据
            		   }//更新表格数据 
               $("#mxtableId").bootstrapTable("updateCell",rowsdwmc);
               
               layer.close(index);
	     		}
        	}
        }
        
        
        /*功能科目*/
        function selectysKmdmTree(upIndex) {
            var options = {
            	title: '预算科目'+'_'+upIndex,
                width: "420",
                url: ctx + "system/yskm/selectYskmForlbTree/103",
                callBack: yskmSubmit
            };
            $.modal.openOptions(options);
        }

        function yskmSubmit(index, layero){
        	var tree = layero.find("iframe")[0].contentWindow.$._tree;
			if ($.tree.notAllowParents(tree)) {
			   var titile=$(".layui-layer-title")[0].innerText;
	     	   var rowIndex=0;
	     	   if (titile!=undefined){
	     		   rowIndex=titile.split('_')[1];
               var body = layer.getChildFrame('body', index);
               var yskmdm=body.find('#treeId').val();
               var yskmdmName=body.find('#treeTitle').val();
               var rowsyskm = {
            		   index : rowIndex, //更新列所在行的索引
            		   field : "yskm", //要更新列的field
            		   value : yskmdm //要更新列的数据
            		   }//更新表格数据 
               $("#mxtableId").bootstrapTable("updateCell",rowsyskm);
               var rowsyskmmc = {
            		   index : rowIndex, //更新列所在行的索引
            		   field : "yskmmc", //要更新列的field
            		   value : yskmdmName //要更新列的数据
            		   }//更新表格数据 
               $("#mxtableId").bootstrapTable("updateCell",rowsyskmmc);
               layer.close(index);
				}
        	}
        }
        
        /*会计科目-新增-选择父部门树*/
        function selectxmxxTree(upIndex,dwbm) {
        	if (dwbm==undefined|| dwbm==''){
        		 layer.alert("请先选择单位", {
      		        icon: 0,
      		        title: "系统提示"
 	     		 })
 	     		 return;
        	} 
            var options = {
            	title: '项目'+'_'+upIndex,
                width: "380",
                url: ctx + "system/xmxx/selectXmxxTree/"+dwbm,
                callBack: doxmxxSubmit
            };
            $.modal.openOptions(options);
        }

        function doxmxxSubmit(index, layero){
        	var tree = layero.find("iframe")[0].contentWindow.$._tree;
			//if ($.tree.notAllowParents(tree)) {
        	   var titile=$(".layui-layer-title")[0].innerText;
	     	   var rowIndex=0;
	     	   if (titile!=undefined){
	     		   rowIndex=titile.split('_')[1];
	     	   }
               var body = layer.getChildFrame('body', index);
               var xmbm=body.find('#treeId').val();
               var xmmcTitle=body.find('#treeTitle').val();
               var xmmc=xmmcTitle.split('^')[0];
               var rowskmdm = {
            		   index : rowIndex, //更新列所在行的索引
            		   field : "xmbm", //要更新列的field
            		   value : xmbm //要更新列的数据
            		   }//更新表格数据 
               $("#mxtableId").bootstrapTable("updateCell",rowskmdm);
         	   var kmdmName = {
                 		   index : rowIndex, //更新列所在行的索引
                 		   field : "xmmc", //要更新列的field
                 		   value : xmmc //要更新列的数据
                 		   }//更新表格数据 
              $("#mxtableId").bootstrapTable("updateCell",kmdmName);
         	  if (xmmcTitle.split('^').length==3){
         		  debugger;
	         	  var yskmdm=xmmcTitle.split('^')[1];
	              var yskmdmName=xmmcTitle.split('^')[2];
	              var rowsyskm = {
	           		   index : rowIndex, //更新列所在行的索引
	           		   field : "yskm", //要更新列的field
	           		   value : yskmdm //要更新列的数据
	           		   }//更新表格数据 
	              $("#mxtableId").bootstrapTable("updateCell",rowsyskm);
	              var rowsyskmmc = {
	           		   index : rowIndex, //更新列所在行的索引
	           		   field : "yskmmc", //要更新列的field
	           		   value : yskmdmName //要更新列的数据
	           		   }//更新表格数据 
	              $("#mxtableId").bootstrapTable("updateCell",rowsyskmmc);
         	   }
               layer.close(index);
			//}
        }
    </script>
</body>
</html>